package artid.com.arttogo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.AppItem;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;

public class RegisterActivity extends BaseActivity implements OnClickListener {
	private Intent mIntent;
	private TextView mNext;
	private EditText name, phone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initView();
	}

	private void initView() {
		mContext = this;
		mBack = (ImageView) findViewById(R.id.back_btn);
		mTittle = (TextView) findViewById(R.id.tittle_text);
		mNext = (TextView) findViewById(R.id.next_btn);
		name = (EditText) findViewById(R.id.et_user_name);
		phone = (EditText) findViewById(R.id.et_user_phone);
		mBack.setOnClickListener(this);
		mNext.setOnClickListener(this);
		mTittle.setText("注册");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_btn:
			finish();
			break;
		case R.id.next_btn:
			next();

			break;

		default:
			break;
		}
	}

	/**
	 * 
	 */
	private void next() {
		// TODO Auto-generated method stub
		if (TextUtils.isEmpty(name.getText().toString())) {
			Toast.makeText(mContext, "姓名不能为空~", 2).show();
			return;
		} else if (TextUtils.isEmpty(phone.getText().toString())) {
			Toast.makeText(mContext, "手机号不能为空~", 2).show();
			return;
		} else if (phone.getText().toString().length()!=11) {
			Toast.makeText(mContext, "手机号格式不正确，请重新输入~", 2).show();
			return;
		}
		VolleyBasic.getInstance(RegisterActivity.this).post(Cache.sendregcode,
				VolleyMap.getInstance().sendregcode(phone.getText().toString()), AppItem.class, new VolleyCallback() {
					
					@Override
					public void onSuccess(VolleyData vd) {
						// TODO Auto-generated method stub
						
						mIntent = new Intent(mContext, RegisterInputActivity.class);
						mIntent.putExtra("phone", phone.getText().toString());
						mIntent.putExtra("name", name.getText().toString());
						startActivity(mIntent);
					}
					
					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						
					}
				});
	}

}
